package com.leap.mini;

import com.leap.service.AdminService;
import com.leap.utils.BeanUtils;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

public class AdminAuthenticationProvider  implements AuthenticationProvider
{
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = authentication.getPrincipal().toString();
        String password = authentication.getCredentials().toString();

        AdminService adminService = BeanUtils.getBean(AdminService.class);
        UserDetails admin = adminService.loadUserByUsername(username);

        UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(username,password,admin.getAuthorities());
        result.setDetails(admin);
        return result;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
    }
}
